/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.brands.application.ics.delegate;

import br.com.brands.application.ics.beans.BrandsBeans;
import br.com.brands.application.ics.factory.DAOFactory;
import java.io.Serializable;
import java.util.List;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.hibernate.HibernateException;

/**
 *
 * @author ppacheco
 */
public class BrandsDelegate implements Serializable {

    /**
     *
     */
    private static final long serialVersionUID = 2825432795066120739L;
    private static BrandsDelegate instance = null;
    private static final Logger log = Logger.getLogger(BrandsDelegate.class
            .getName());

    public BrandsDelegate() {
        log.log(Level.INFO, "Loading BrandsDelegate.");
    }

    public BrandsBeans save(BrandsBeans brands) throws HibernateException,
            InstantiationException {
        try {
            log.log(Level.INFO, "Executing at Delegate layer level");
            if (brands != null) {
                DAOFactory.getInstance().getBrandsDAOImpl().save(brands);
            }
        } catch (Exception e) {
            log.log(Level.ERROR, e.getMessage(), e);
        }
        return brands;
    }

    public BrandsBeans saveOrUpdate(BrandsBeans brands)
            throws HibernateException, InstantiationException {
        try {
            log.log(Level.INFO, "Executing at Delegate layer level");
            if (brands != null) {
                DAOFactory.getInstance().getBrandsDAOImpl()
                        .saveOrUpdate(brands);
            }
        } catch (Exception e) {
            log.log(Level.ERROR, e.getMessage(), e);
        }
        return brands;
    }

    public BrandsBeans delete(BrandsBeans brands) throws HibernateException,
            InstantiationException {
        try {
            log.log(Level.INFO, "Executing at Delegate layer level");
            if (brands != null) {
                DAOFactory.getInstance().getBrandsDAOImpl().delete(brands);
            }
        } catch (Exception e) {
            log.log(Level.ERROR, e.getMessage(), e);
        }
        return brands;
    }

    public BrandsBeans findByID(BrandsBeans brands, Long id, boolean lock)
            throws HibernateException, InstantiationException {

        BrandsBeans objRef = null;

        try {
            if (id != null) {
                log.log(Level.INFO, "Executing at Delegate layer level");
                objRef = (BrandsBeans) DAOFactory.getInstance()
                        .getBrandsDAOImpl().findByID(brands, id, lock);
            }
        } catch (Exception e) {
            log.log(Level.ERROR, e.getMessage(), e);
        }
        return objRef;
    }

    @SuppressWarnings("unchecked")
    public List<BrandsBeans> findAll(BrandsBeans brands)
            throws HibernateException, InstantiationException {

        List<BrandsBeans> list = null;

        try {
            if (brands != null) {
                log.log(Level.INFO, "Executing at Delegate layer level");
                list = DAOFactory.getInstance().getBrandsDAOImpl()
                        .findAll(brands);
            }
        } catch (Exception e) {
            log.log(Level.ERROR, e.getMessage(), e);
        }
        return list;
    }

    public static BrandsDelegate getInstance() throws InstantiationException {
        try {
            if (instance == null) {
                instance = new BrandsDelegate();
            }
        } catch (Exception e) {
            log.log(Level.ERROR, e.getMessage(), e);
        }
        return instance;
    }
}
